package com.xyyl.patient.service.archives;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.xyyl.patient.core.constant.AppEnum;
import com.xyyl.patient.entity.archives.Archives;
import com.xyyl.patient.entity.archives.Visit;
import com.xyyl.patient.mapper.archives.ArchivesMapper;
import com.xyyl.patient.vo.ArchivesVo;

/**
 * @ClassName: ArchivesServiceImp
 * @Description: 患者档案Service
 * @author fangsheng
 * @date 2017年8月17日
 *
 */
@Service("archivesService")
public class ArchivesServiceImp implements ArchivesService {

	@Autowired
	private ArchivesMapper archivesMapper;

	@Override
	public List<ArchivesVo> selectList(Integer patientid) {

		List<ArchivesVo> archivesVos = Lists.newArrayList();

		List<Archives> archivesS = archivesMapper.queryArchivesList(patientid);
		if (archivesS != null && !archivesS.isEmpty()) {
			ArchivesVo archivesVo = null;
			for (Archives archives : archivesS) {
				archivesVo = new ArchivesVo();
				BeanUtils.copyProperties(archives, archivesVo);
				archivesVo.setName(archives.getName());
				archivesVo.setSex(AppEnum.SexEnum.getName(archives.getSex()));
				archivesVo.setAge(archives.getAge());
				archivesVo.setBirthday(archives.getBirthday());
				archivesVo.setPhoneNumber(archives.getPhoneNumber());
				archivesVo.setAllergydrugs(archives.getAllergydrugs());
				archivesVos.add(archivesVo);
			}
		}
		return archivesVos;
	}

	@Override
	public int insertArchives(ArchivesVo map) {
		Archives archives = new Archives();
		int a = 0;
		try {
			if (null != map) {
				archives.setPatientid(map.getPatientid());
				archives.setAge(map.getAge());
				archives.setBirthday(map.getBirthday());
				archives.setName(map.getName());
				archives.setSex(Integer.valueOf(map.getSex()));
				archives.setPhoneNumber(map.getPhoneNumber());
				archives.setType(map.getType());
				archives.setOurself(map.getOurself());
				a = archivesMapper.insert(archives);
				return a;
			}
		} catch (Exception e) {

		}
		return a;
	}

	/**
	 * 
	 * @Description: 更新患者档案
	 * @author fangsheng
	 * @date 2017年8月17日
	 *
	 */
	@Override
	public Integer updateArchives(ArchivesVo map) {
		// 更新archives信息
		Archives archives = new Archives();
		if (map != null) {
			archives.setPatientid(map.getPatientid());
			archives.setId(map.getId());
			archives.setAge(map.getAge());
			archives.setBirthday(map.getBirthday());
			archives.setSex(Integer.valueOf(map.getSex()));
			archives.setName(map.getName());
			archives.setPhoneNumber(map.getPhoneNumber());
			Integer resultInsert = archivesMapper.update(map);
			return resultInsert;

		} else {
			return 0;
		}

	}

	/**
	 * 
	 * @Description: 删除患者档案
	 * @author fangsheng
	 * @date 2017年8月22日
	 *
	 */
	@Override
	public int deleteArchives(Integer id) {
		ArchivesVo archives = new ArchivesVo();
		if (id != null) {
			archives.setId(id);
			// 删除 0：正常、1：已删除
			archives.setIsDeleted(AppEnum.common.archives__type_delete.getValue());
			return archivesMapper.update(archives);
		}
		return 0;
	}

	/**
	 * 
	 * @Description: 校验患者档案
	 * @author fangsheng
	 * @date 2017年8月22日
	 *
	 */
	@Override
	public List<Archives> checkArchives(Integer patientid) {

		List<Archives> list = archivesMapper.queryArchivesList(patientid);
		if (list != null && !list.isEmpty()) {
			// 用户档案已存在
			return list;
		} else {
			// 用户档案不存在
			return null;
		}

	}

	/**
	 * 查询电子档案
	 */
	@Override
	public List<Archives> selectElectronicArchivesList(Integer patientid) {
		return archivesMapper.queryArchivesList(patientid);
	}

	/**
	 * 新增电子档案
	 */
	@Override
	public int insertElectronicArchives(Archives archives) {

		return archivesMapper.insert(archives);
	}


	@Override
	public int updateElectronicArchives(Archives archives) {

		return archivesMapper.update(archives);
	}

	@Override
	public Archives selectOne(Integer id) {
		return archivesMapper.selectOne(id);
	}

	@Override
	public List<Archives> checkVisit(Integer id) {
		return archivesMapper.selectById(id);
	}

	@Override
	public List<Visit> queryListTern(Integer archivesId) {
		return archivesMapper.queryListTern(archivesId);
	};

}
